package com.yanqu.road.dao.impl.wingroom;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.CrossUserVisitRewardData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUserVisitRewardDataDaoImpl extends TempDataObjectDao<CrossUserVisitRewardData> {


    public static String getCrossUserVisitRewardDataSql = "select * from `t_c_wingroom_patrons_visit_reward` ";
    public Map<Long, CrossUserVisitRewardData> getCrossUserVisitRewardData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossUserVisitRewardData> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossUserVisitRewardDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUserVisitRewardData data = parse(rs);
                resultMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    public static String insertCrossUserVisitRewardDataSql = "insert into `t_c_wingroom_patrons_visit_reward`(userId,endTime,reward,isGet,homeUserId) values (?,?,?,?,?)";

    @Override
    protected boolean addData(CrossUserVisitRewardData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGet()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHomeUserId()));
        if (getDb().executeNoneQuery(insertCrossUserVisitRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossUserVisitRewardDataSql = "update `t_c_wingroom_patrons_visit_reward` set endTime=?,reward=?,isGet=?,homeUserId=? where userId=?";

    @Override
    protected boolean updateData(CrossUserVisitRewardData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGet()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHomeUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateCrossUserVisitRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossUserVisitRewardData parseData(ResultSet rs) throws SQLException {
        CrossUserVisitRewardData data = new CrossUserVisitRewardData();
        data.setUserId(rs.getLong("userId"));
        data.setEndTime(rs.getInt("endTime"));
        data.setReward(rs.getString("reward"));
        data.setGet(rs.getBoolean("isGet"));
        data.setHomeUserId(rs.getLong("homeUserId"));

        return data;
    }
}
